{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三大结构\n",
    "- 顺序\n",
    "- 分支\n",
    "- 循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分支\n",
    "- 分支的基本语法\n",
    "\n",
    "         if 条件表达式：\n",
    "             语句1 \n",
    "             语句2\n",
    "             语句3\n",
    "             ......\n",
    "- 条件表达式就是计算结果必须为布尔值的表达式\n",
    "- 表达式后面的冒号不能少\n",
    "- 注意if后面的出现的语句，如果属于if语句块，则必须锁紧等级\n",
    "- 条件表达式为true执行if后面的缩进语句\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我们不带你玩\n",
      "滚蛋\n"
     ]
    }
   ],
   "source": [
    "# if语句练习\n",
    "# 如果age小于18，则打印信息“去死”\n",
    "age = 19\n",
    "if age < 18:\n",
    "    print(\"去死\")\n",
    "print(\"我们不带你玩\")\n",
    "print(\"滚蛋\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 双向分支\n",
    "- if...else...语句\n",
    "         if 条件表达式：\n",
    "              语句1\n",
    "              语句2\n",
    "         else:\n",
    "              语句1\n",
    "              语句2\n",
    "              .......\n",
    "- 双向分支有两个分支，当程序执行到if...else...语句的时候，一定会执行if或者else的一个，也仅执行一个\n",
    "- 缩进问题，if和else一个层级，其余语句一个层级"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入性别：nan\n",
      "你输入的性别是nan\n",
      "新的一天，新的难过\n",
      "我们不一样\n"
     ]
    }
   ],
   "source": [
    "# input的作用\n",
    "# 1、在屏幕上输出括号内的字符串\n",
    "# 2、在接受用户输入的内容并返回到程序\n",
    "# 3、input返回的内容一定是字符串类型\n",
    "\n",
    "gender = input(\"请输入性别：\")\n",
    "print( \"你输入的性别是{0}\".format(gender))\n",
    "if gender == \"nan\":\n",
    "    print(\"新的一天，新的难过\")\n",
    "else:\n",
    "    print(\"给大佬端茶\")\n",
    "print(\"我们不一样\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入学生的成绩90\n",
      "A\n"
     ]
    }
   ],
   "source": [
    "# score 存放学生成绩\n",
    "# 注意input的返回值类型\n",
    "score = input(\"请输入学生的成绩\")\n",
    "# 成绩比较 字符串转化成Int\n",
    "score = int(score)\n",
    "if score >= 90:\n",
    "    print(\"A\")\n",
    "if score >= 80 and score <90:\n",
    "    print(\"B\")\n",
    "if score >= 70 and score <80:\n",
    "    print(\"C\")\n",
    "if score >= 60 and score <70:\n",
    "    print(\"D\")\n",
    "if score < 60:\n",
    "    print(\"wo mei you ni zhe ge xue sheng\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多路分支\n",
    "- 很多分支的情况，简称多路分支\n",
    "         \n",
    "         if 条件表达式：\n",
    "            语句1\n",
    "            语句2\n",
    "         elif:\n",
    "            语句1\n",
    "            语句2\n",
    "         elif:  \n",
    "            语句1\n",
    "            语句2\n",
    "         elif:\n",
    "            语句1\n",
    "            语句2\n",
    "         else:\n",
    "           语句1\n",
    "           语句2\n",
    "\n",
    "- elif 可以有很多个\n",
    "- else 可选\n",
    "- 多路分支只会选择 执行一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入学生的成绩30\n",
      "wo mei you ni zhe ge xue sheng\n"
     ]
    }
   ],
   "source": [
    "# score 存放学生成绩\n",
    "# 注意input的返回值类型\n",
    "score = input(\"请输入学生的成绩: \")\n",
    "# 成绩比较 字符串转化成Int\n",
    "score = int(score)\n",
    "if score >= 90:\n",
    "    print(\"A\")\n",
    "elif score >= 80:\n",
    "    print(\"B\")\n",
    "elif score >= 70:\n",
    "    print(\"C\")\n",
    "elif score >= 60:\n",
    "    print(\"D\")\n",
    "else:\n",
    "    print(\"wo mei you ni zhe ge xue sheng\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## if语句其它：\n",
    "- if语句可以嵌套使用，但不推荐使用\n",
    "- python没有switch-case语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环语句\n",
    "- 重复执行某些固定的动作或处理固定的事物\n",
    "- 分类\n",
    "    - for循环\n",
    "    - while循环\n",
    "## for循环\n",
    "- for循环\n",
    "        for 变量 in 序列:\n",
    "             语句1\n",
    "             语句2\n",
    "             ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wew\n",
      "wewfe\n",
      "wefwaf\n"
     ]
    }
   ],
   "source": [
    "#打印列表\n",
    "for name in [\"wew\",\"wewfe\",\"wefwaf\"]:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## range介绍\n",
    "- 生成一个数字序列\n",
    "- 具体范围可以设定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# range练习\n",
    "# 打印1-10的数字\n",
    "# 注意，一般在python钟，如果表示数字范围的两个数，一般是包含左边数字不包含右边数字（左闭右开）\n",
    "# randint是特例，左右都包含\n",
    "for i in range (1,10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for-else语句\n",
    "- 当for循环结束的时候会执行else语句\n",
    "- else语句是可选语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wew\n",
      "wo shi shei\n",
      "wewfe\n",
      "ni shi sha bi\n",
      "wefwaf\n",
      "ni shi sha bi\n",
      "wo men zai yi ma\n"
     ]
    }
   ],
   "source": [
    "# for-else 语句\n",
    "#  打印列表中的同学\n",
    "# 如果灭有在列表中，或者列表结束了，我们需要打印提示语句\n",
    "\n",
    "for name in [\"wew\",\"wewfe\",\"wefwaf\"]:\n",
    "    print(name)\n",
    "    if name == \"wew\":\n",
    "        print(\"wo shi shei\")\n",
    "    else:\n",
    "        print(\"ni shi sha bi\")\n",
    "else:\n",
    "    print(\"wo men zai yi ma\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环之break，contineu，pass\n",
    "- break： 无条件结束整个循环\n",
    "- continue： 无条件结束本次循环，重新进了入下一轮循环\n",
    "- pass：表示略过"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "我找到了\n"
     ]
    }
   ],
   "source": [
    "# 在1-10的数字中，寻找数字7，一旦找到，打印出来，其余则什么都不做\n",
    "# for 循环中的变量表示，一般用i、k、m、n或者indx、idx、item之类\n",
    "# 当变量不需要引用的时候可以用（_）代替 for _ in var\n",
    "for i in range(1,11):\n",
    "    if i == 7:\n",
    "        print(\"我找到了\")\n",
    "        break\n",
    "    else:\n",
    "        print(i)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 是偶数\n",
      "4 是偶数\n",
      "6 是偶数\n",
      "8 是偶数\n",
      "10 是偶数\n",
      "2 是偶数\n",
      "4 是偶数\n",
      "6 是偶数\n",
      "8 是偶数\n",
      "10 是偶数\n"
     ]
    }
   ],
   "source": [
    "# continue 语句联系 版本1\n",
    "# 在1-10的数字中，寻找偶数，找到后进行打印\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i % 2 == 1:\n",
    "        continue\n",
    "    else:\n",
    "        print(\"{0} 是偶数\".format(i))\n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "         \n",
    "# continue 语句联系  版本2\n",
    "# 在1-10的数字中，寻找偶数，找到后进行打印\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i % 2 == 1:\n",
    "        continue\n",
    "    \n",
    "    print(\"{0} 是偶数\".format(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pass例子 一般用于占位\n",
    "#pass没有跳过的功能\n",
    "for i in range(1,10):\n",
    "    pass\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
